package com.jxb.tranction;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;

//SHOW  PROCESSLIST 查看mysql的连接数
@Component
public class TxDemoService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private TxDemoService txDemoService;

    @Transactional
    public void test() {
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
            @Override
            public void suspend() {
                System.out.println("test被挂起了");
            }

            @Override
            public void resume() {
                System.out.println("test被恢复了");
            }

            @Override
            public void beforeCommit(boolean readOnly) {
                System.out.println("test准备要提交了");
            }

            @Override
            public void beforeCompletion() {
                System.out.println("test准备要提交或回滚了");
            }

            @Override
            public void afterCommit() {
                System.out.println("test提交成功了");
            }

            @Override
            public void afterCompletion(int status) {
                System.out.println("test提交或回滚成功了");
            }
        });
        jdbcTemplate.execute("INSERT INTO `test`.`user` (`id`, `name`) VALUES (1, '1');");
        jdbcTemplate.execute("INSERT INTO `test`.`user` (`id`, `name`) VALUES (2, '1');");
        // txDemoService.a();
        txDemoService.b();
        // jdbcTemplate.execute("INSERT INTO `test`.`user` (`id`, `name`) VALUES (4, '1');");
        // jdbcTemplate.execute("INSERT INTO `test`.`user` (`id`, `name`) VALUES (5, '1');");


        // txDemoService.c();
        //这样使用，spring会事务生效，因为执行的是一个普通方法
//        a();
//        try {

//        }catch (Exception e) {
//            //强制spring事务抛出异常
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//            System.out.println("系统出了小差，稍后重试... ...");
//        }
    }

    @Transactional
   // @Transactional(propagation = Propagation.REQUIRES_NEW)
//    @Transactional(propagation = Propagation.NEVER)
//    @Transactional(propagation = Propagation.NESTED)
//     @Transactional(propagation = Propagation.NOT_SUPPORTED)//将当前事务挂起
    public void a() {
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
            @Override
            public void suspend() {
                System.out.println("a被挂起了");
            }

            @Override
            public void resume() {
                System.out.println("a被恢复了");
            }

            @Override
            public void beforeCommit(boolean readOnly) {
                System.out.println("a准备要提交了");
            }

            @Override
            public void beforeCompletion() {
                System.out.println("a准备要提交或回滚了");
            }

            @Override
            public void afterCommit() {
                System.out.println("a提交成功了");
            }

            @Override
            public void afterCompletion(int status) {
                System.out.println("a提交或回滚成功了");
            }
        });
        jdbcTemplate.execute("INSERT INTO `test`.`user` (`id`, `name`) VALUES (2, '2');");
    }

    @Transactional(propagation = Propagation.NESTED)
    public void b() {
        jdbcTemplate.execute("INSERT INTO `test`.`user` (`id`, `name`) VALUES (3, '2');");
        int i = 100/0;
    }
    @Transactional
    public void c() {
        jdbcTemplate.execute("INSERT INTO `test`.`user` (`id`, `name`) VALUES (4, '2');");
        int i = 100/0;
    }

}
